#include "genenetwork.h"
#include "ui_genenetwork.h"
#include "graph.h"
#include "biotablewidget.h"
#include "attributeset.h"

GeneNetwork::GeneNetwork(QString fn, QGraphicsItem *parent) :
     filename(fn), QGraphicsWidget(parent)
    //ui(new Ui::GeneNetwork)

{
    graph = new Graph( this );
    graph->setPos( 0 , 0);
   // ui->setupUi(this);
    qDebug() << "filename = " << filename << endl;
    //GeneNetworkReader reader( filename );

    readFile(filename);
    qDebug() << "file loaded" << endl;
    graph->applyForceDirectedLayout();

    setFlag( QGraphicsItem::ItemClipsChildrenToShape );
    setFlag( QGraphicsItem::ItemIsMovable );
}

GeneNetwork::~GeneNetwork()
{
    //delete ui;
}

GeneNetwork::GeneNetwork(const GeneNetwork& copy)
    : graph( copy.graph )
{
    setFlag( QGraphicsItem::ItemClipsChildrenToShape );
    setFlag( QGraphicsItem::ItemIsMovable );
}


QRectF GeneNetwork::boundingRect() const
{
    return QRectF(-500, -300, 1000, 600); //note- before attributes added, was width = 700
}

QPainterPath GeneNetwork::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}


void GeneNetwork::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QColor background(50, 50, 50, 255 );
    painter->setBrush(background);

    QRectF bRect = boundingRect();
    bRect.adjust(1, 1, -1, -1);
    painter->drawRect(bRect);//0, 0, 598, 598);//-299, -299, 598, 598 );
}

bool GeneNetwork::readFile(QString filename)
{
    QFile f(filename);

    if( !f.exists() )
    {
        qDebug() << "file not found." << endl;
        return false;
    }

    if( ! f.open(QIODevice::ReadOnly))
    {
        qDebug() << "file " << filename << " can't be opened." << endl;
        return false;
    }

    qDebug() << "file " << filename << " opened." << endl;

    QTextStream stream(&f);
    QString line;
    do {
         line = stream.readLine();
         qDebug() << "line =  " << line << endl;
         QStringList list = line.split(QRegExp("\\s+"), QString::SkipEmptyParts);
         if( list.size() == 3 )
            graph->addEdge( list[0], list[2], list[1] );
     } while (!stream.atEnd() );

    return true;
}

//connect one network to another
void GeneNetwork::forgeConnection(GeneNetwork *other)
{
    graph->forgeConnection( other->graph );
}

//Connect BioTable attribute sets to GeneNetwork
void GeneNetwork::forgeConnection(BioTableWidget *table)
{   
    QVector< QPointer<QualAttributeSet> > attributeSets;
    table->getAttributeSets(attributeSets);
    qDebug() << attributeSets.size() << endl;
    for( int i = 0; i < attributeSets.size(); i++)
    {
        attributeSets[i]->setVisible( true );
        addAttributeSet( attributeSets[i] );
    }
}

void GeneNetwork::addAttributeSet( QPointer<QualAttributeSet> attrs)
{
    attributeSets.push_back( attrs );
    attributeSets[ attributeSets.size()-1 ]->setParentItem(this);
    attributeSets[ attributeSets.size()-1 ]->setPos( 400, -300+(attributeSets.size()-1)*200+100 );
    attributeSets[ attributeSets.size()-1 ]->setVisible(true);
    qDebug() << "adding attribute set to network: " << endl;
    qDebug() << *attrs << endl;

    for(int i = 0; i < attrs->getNumberOfAttributes(); i++)
    {
        graph->addAttribute(attrs->getAttribute(i) );
    }
    update();
}
